package org.msfj.generator;

import cn.hutool.core.date.DateUtil;
import org.apache.velocity.VelocityContext;

import java.io.File;
import java.util.Map;

public class CodeGenerateFactory {
    private static String moduleName;
    private static String fetchMethod;
    private static String basePackage;
    private static String modelPackage;
    private static String mapperPackage;
    private static String voPackage;
    private static String voPath;
    private static String servicePackage;
    private static String servicePath;
    private static String serviceImplPackage;
    private static String serviceImplPath;
    private static String controllerPackage;
    private static String controllerPath;
    private static String apiPath;
    private static String vuePath;
    private static boolean isTree = false;
    private static Map<String, Column> columns;

    private static VelocityContext getVelocityContext(String tableName, String aliasName) {
        TableUtil tableUtil = new TableUtil();
        String className = tableUtil.getTablesNameToClassName(tableName);
        String lowerName = className.substring(0, 1).toLowerCase() + className.substring(1, className.length());
        VelocityContext context = new VelocityContext();
        context.put("moduleName", moduleName);
        context.put("aliasName", aliasName);
        context.put("className", className);
        context.put("lowerName", lowerName);
        context.put("voName", className + "Vo");
        context.put("lowerVoName", lowerName + "Vo");
        context.put("basePackage", basePackage);
        context.put("modelPackage", modelPackage);
        context.put("mapperPackage", mapperPackage);
        context.put("voPackage", voPackage);
        context.put("servicePackage", servicePackage);
        context.put("serviceImplPackage", serviceImplPackage);
        context.put("controllerPackage", controllerPackage);
        context.put("date", DateUtil.now());
        context.put("fetchMethod",fetchMethod);
        return context;
    }

    public static void codeGenerateVo(String tableName, String aliasName) {
        VelocityContext context = getVelocityContext(tableName, aliasName);
        context.put("isTree", isTree);
        String separator = File.separator;
        String voFile = voPackage.replace(".", separator) + separator + context.get("className") + "Vo.java";
        CommonPageParser.WriterPage(context, "VoTemplate.vm", voPath + separator, voFile);
    }

    public static void codeGenerateServiceImpl(String tableName, String aliasName) {
        VelocityContext context = getVelocityContext(tableName, aliasName);
        context.put("isTree", isTree);
        String separator = File.separator;
        String serviceFile = servicePackage.replace(".", separator) + separator + context.get("className") + "Service.java";
        String serviceImplFile = serviceImplPackage.replace(".", separator) + separator + context.get("className") + "ServiceImpl.java";
        CommonPageParser.WriterPage(context, "ServiceTemplate.vm", servicePath + separator, serviceFile);
        CommonPageParser.WriterPage(context, "ServiceImplTemplate.vm", serviceImplPath + separator, serviceImplFile);
    }

    public static void codeGenerateController(String tableName, String aliasName) {
        VelocityContext context = getVelocityContext(tableName, aliasName);
        context.put("isTree", isTree);
        String separator = File.separator;
        String controllerFile = controllerPackage.replace(".", separator) + separator + context.get("className") + "Controller.java";
        CommonPageParser.WriterPage(context, "ControllerTemplate.vm", controllerPath + separator, controllerFile);
    }

    public static void codeGenerateHtml(String tableName, String aliasName) {
        VelocityContext context = getVelocityContext(tableName, aliasName);
        context.put("isTree", isTree);
        context.put("columns", columns);
        String separator = File.separator;
        String apiFile = context.get("lowerName") + ".js";
        context.put("apiPath", apiPath.substring(apiPath.indexOf("api") + 3, apiPath.length()).replace("\\", "/") + "/" + moduleName + "/" + context.get("lowerName"));
        String vueFile = context.get("lowerName") + ".vue";
        CommonPageParser.WriterPage(context, "vue-api.vm", apiPath + separator + moduleName + separator, apiFile);
        if (isTree) {
            CommonPageParser.WriterPage(context, "vue-tree.vm", vuePath + separator + moduleName + separator, vueFile);
        } else {
            CommonPageParser.WriterPage(context, "vue.vm", vuePath + separator + moduleName + separator, vueFile);
        }
    }

    public static void setModuleName(String moduleName) {
        CodeGenerateFactory.moduleName = moduleName;
    }

    public static void setBasePackage(String basePackage) {
        CodeGenerateFactory.basePackage = basePackage;
    }

    public static void setModelPackage(String modelPackage) {
        CodeGenerateFactory.modelPackage = modelPackage;
    }

    public static void setMapperPackage(String mapperPackage) {
        CodeGenerateFactory.mapperPackage = mapperPackage;
    }

    public static void setVoPackage(String voPackage) {
        CodeGenerateFactory.voPackage = voPackage;
    }

    public static void setControllerPackage(String controllerPackage) {
        CodeGenerateFactory.controllerPackage = controllerPackage;
    }

    public static void setVoPath(String voPath) {
        CodeGenerateFactory.voPath = voPath;
    }

    public static void setservicePackage(String servicePackage) {
        CodeGenerateFactory.servicePackage = servicePackage;
    }

    public static void setservicePath(String servicePath) {
        CodeGenerateFactory.servicePath = servicePath;
    }

    public static void setserviceImplPackage(String serviceImplPackage) {
        CodeGenerateFactory.serviceImplPackage = serviceImplPackage;
    }

    public static void setserviceImplPath(String serviceImplPath) {
        CodeGenerateFactory.serviceImplPath = serviceImplPath;
    }

    public static void setControllerPath(String controllerPath) {
        CodeGenerateFactory.controllerPath = controllerPath;
    }

    public static void setVuePath(String vuePath) {
        CodeGenerateFactory.vuePath = vuePath;
    }

    public static void setColumns(Map<String, Column> columns) {
        CodeGenerateFactory.columns = columns;
    }

    public static void setApiPath(String apiPath) {
        CodeGenerateFactory.apiPath = apiPath;
    }

    public static void setIsTree(boolean isTree) {
        CodeGenerateFactory.isTree = isTree;
    }

    public static void setFetchMethod(String fetchMethod) {
        CodeGenerateFactory.fetchMethod = fetchMethod;
    }

    public static void main(String[] args) {
        String aa = "D:\\code\\web\\base4j-ui\\src\\api\\test";
        System.out.println(aa);
    }
}